import { NodeName } from "../lang/zh";

// 创建监听器实例
export function createListenerObject(moddle, options, isTask, prefix) {
  const {
    event,
    id,
    listenerType,
    expression,
    delegateExpression,
    class: clazz,
    fields,
    eventDefinitionType,
    eventTimeDefinitions,
  } = options;
  const listenerObj = {
    event,
    ...(isTask && { id }),
  };

  switch (listenerType) {
    case "scriptListener":
      listenerObj.script = createScriptObject(moddle, options, prefix);
      break;
    case "expressionListener":
      listenerObj.expression = expression;
      break;
    case "delegateExpressionListener":
      listenerObj.delegateExpression = delegateExpression;
      break;
    default:
      listenerObj.class = clazz;
  }

  if (fields) {
    listenerObj.fields = fields.map((field) =>
      createFieldObject(moddle, field, prefix)
    );
  }

  if (isTask && event === "timeout" && eventDefinitionType) {
    const timeDefinition = moddle.create("bpmn:FormalExpression", {
      body: eventTimeDefinitions,
    });
    const TimerEventDefinition = moddle.create("bpmn:TimerEventDefinition", {
      id: `TimerEventDefinition_${uuid(8)}`,
      [`time${eventDefinitionType.replace(/^\S/, (s) => s.toUpperCase())}`]:
        timeDefinition,
    });
    listenerObj.eventDefinitions = [TimerEventDefinition];
  }
  // console.log(prefix,'prefixprefix');
  // console.log(isTask,'isTaskisTaskisTask')

  return moddle.create(
    `${prefix}:${isTask ? "TaskListener" : "ExecutionListener"}`,
    listenerObj
  );
}

// 处理内置流程监听器
export function createSystemListenerObject(moddle, options, isTask, prefix) {
  const { eventType, valueType, expression, delegateExpression, value } =
    options;
  const listenerObj = {
    event: eventType,
    listenerType: valueType,
  };

  switch (valueType) {
    case "scriptListener":
      listenerObj.script = createScriptObject(moddle, options, prefix);
      break;
    case "expressionListener":
      listenerObj.expression = expression;
      break;
    case "delegateExpressionListener":
      listenerObj.delegateExpression = delegateExpression;
      break;
    default:
      listenerObj.class = value;
  }

  return moddle.create(
    `${prefix}:${isTask ? "TaskListener" : "ExecutionListener"}`,
    listenerObj
  );
}

// 转换成字段
export function changeListenerObject(options) {
  const { eventType, valueType, expression, delegateExpression, value } =
    options;
  const listenerObj = {
    event: eventType,
    listenerType: valueType,
  };

  switch (valueType) {
    case "scriptListener":
      // listenerObj.script = createScriptObject(moddle, options, prefix);
      break;
    case "expressionListener":
      listenerObj.expression = expression;
      break;
    case "delegateExpressionListener":
      listenerObj.delegateExpression = delegateExpression;
      break;
    default:
      listenerObj.class = value;
  }

  return listenerObj;
}

// 创建监听器的注入字段实例
export function createFieldObject(moddle, option, prefix) {
  const { name, fieldType, string, expression } = option;
  const fieldConfig =
    fieldType === "string" ? { name, string } : { name, expression };
  return moddle.create(`${prefix}:Field`, fieldConfig);
}

// 创建脚本实例
export function createScriptObject(moddle, options, prefix) {
  const { scriptType, scriptFormat, value, resource } = options;
  const scriptConfig =
    scriptType === "inlineScript"
      ? { scriptFormat, value }
      : { scriptFormat, resource };
  return moddle.create(`${prefix}:Script`, scriptConfig);
}

// 更新元素扩展属性
export function updateElementExtensions(
  moddle,
  modeling,
  element,
  extensionList
) {
  const extensions = moddle.create("bpmn:ExtensionElements", {
    values: extensionList,
  });
  console.log(extensions,'extensionsextensions')
  modeling.updateProperties(element, { extensionElements: extensions });
}

// TaskListener


export function updateElementExtensions1(
  moddle,
  modeling,
  element,
  extensionList
) {
  const extensions = moddle.create("bpmn:TaskListener", {
    values: extensionList,
  });
  console.log(extensions,'extensionsextensions')
  modeling.updateProperties(element, { extensionElements: extensions });
}
// 创建一个id
export function uuid(length = 8, chars) {
  const charsString =
    chars || "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return Array.from(
    { length },
    () => charsString[Math.floor(Math.random() * charsString.length)]
  ).join("");
}

// 转换流程节点名称
export function translateNodeName(node) {
  return NodeName[node];
}
